package middleware

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/service"
	"github.com/gin-gonic/gin"
)

func JWT(c *gin.Context) {
	token := c.Request.Header.Get("X-API-Key")
	if len(token) == 0 {
		handler.Errorf(c, "token is missing")
		return
	}

	id, err := service.Parse(token)
	if err != nil {
		handler.Errorf(c, err.Error())
		return
	}

	if config.GetUseTestData() {
		id = 6
	}

	c.Set("UserId", id)
}

func GetUserId(c *gin.Context) uint {
	return c.GetUint("UserId")
}
